package com.xiguaji.tiktok.task.ranking;

import com.gugee.tiktok.common.model.dal.blogger.BloggerRecordRepository;
import com.gugee.tiktok.common.model.mysql.BloggerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Optional;

@Service
public class BloggerRecordService {

    @Autowired
    BloggerRecordRepository bloggerRecordRepository;

    public void updateAndSave(BloggerRecord bloggerRecord){
        bloggerRecordRepository.save(bloggerRecord);
    }

    public List<BloggerRecord> findBloggerRecords(Integer dateCode, String bloggerType){
        if(StringUtils.isEmpty(bloggerType)){
            return bloggerRecordRepository.findTop1000ByDatecodeOrderByFollowerGrowthDesc(dateCode);
        }

        return bloggerRecordRepository.findTop1000ByDatecodeAndBloggerTypeOrderByFollowerGrowthDesc(dateCode,bloggerType);
    }

    public Optional<BloggerRecord> findBloggerRecords(Integer dateCode, long uid){
        return bloggerRecordRepository.findByUidAndDatecode(uid,dateCode);
    }
}
